// web/src/app/api/(social)/channels/[id]/route.ts

import {NextRequest, NextResponse} from "next/server";
import {db} from "@/lib/db";

export async function GET(
    req: NextRequest,
    {params}: { params: { id: string } }
) {
    let id = params.id;
    try {
        const channel = await db.channel.findUnique({
            where: {id}
        })
        return NextResponse.json(channel)
    } catch (e) {
        console.error(e)
        return new NextResponse('Internal Server Error', {status: 500});
    }
}


export async function PATCH(
    req: NextRequest,
    {params}: { params: { id: string } }
) {
    let id = params.id;
    const {...values} = await req.json()
    console.log('update channel', values)

    try {
        const channel = await db.channel.findUnique({
            where: {id}
        })
        if (!channel) return new NextResponse('Not Found', {status: 404});

        const newChannel = await db.channel.update({
            data: values,
            where: {id}
        })

        return NextResponse.json(newChannel)
    } catch (e) {
        console.error(e)
        return new NextResponse('Internal Server Error', {status: 500});
    }
}


export async function DELETE(
    req: NextRequest,
    {params}: { params: { id: string } }
) {
    let id = params.id;
    try {
        const channel = await db.channel.delete({
            where: {id}
        })
        return NextResponse.json(channel)
    } catch (e) {
        console.error(e)
        return new NextResponse('Internal Server Error', {status: 500});
    }
}